/**
 * 功能:TK_CUSTOMS Service类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:50:11
 */
package com.towker.frame.service.basicData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import com.towker.core.db.EntityDao;
import com.towker.core.db.BaseService;
import com.towker.frame.model.basicData.Customs;
import com.towker.frame.model.basicData.Depots;
import com.towker.frame.model.sys.User;
import com.towker.frame.dao.basicData.CustomsDao;
import com.towker.frame.dao.sys.UserDao;

@Service
public class CustomsService extends BaseService<Customs, java.lang.Long>
{
	private CustomsDao dao;
	private UserDao userDao;
	
	public void setCustomsDao(CustomsDao dao)
	{
		this.dao = dao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao()
	{
		return this.dao;
	}
	
	public List<Customs> findByCoCode(String coCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("co_code", coCode);
		return getList(map);
	}
	
	public String getCustomName(boolean isOrder, int userId, String custom_code) {
		boolean fromCustom = true;
		if (isOrder) {
			User user = userDao.getById(userId);
			if (user != null && new Integer(2).equals(user.getUserNature())) {
				fromCustom = false;
			}
		}
		if (fromCustom) {
			Customs customs = dao.getByCustomCode(custom_code);
			if (customs == null) {
				return "";
			} else {
				return customs.getCustom_name();
			}
		} else {
			User user = userDao.getByCustomCode(custom_code);
			if (user != null) {
				return user.getName();
			} else {
				return "";
			}
		}
	}

	public boolean checkExistsCode(String custom_code, Integer id) {
		return dao.checkExistsCode(custom_code, id);
	}
}
